package com.glp.work.flow.controller;

import com.alibaba.fastjson.JSONObject;
import com.glp.fbs.api.dto.account.AccountInfoDto;
import com.glp.fbs.api.dto.appInfo.AppUserResp;
import com.glp.fbs.api.dto.user.PersonInfoWithAppReq;
import com.glp.utils.notify.dto.SendEmail;
import com.glp.work.flow.config.mq.ExchangeQueueConfig;
import com.glp.work.flow.dto.EmailDTO;
import com.glp.work.flow.dto.UserDTO;
import com.glp.work.flow.dto.request.PersonInfoReq;
import com.glp.work.flow.service.UserService;
import com.glp.work.flow.service.WfEmailLogService;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 郭永伟
 * @date : 2020/12/14/15:34
 * @description :
 */
@Slf4j
@Service
public class MessageMQListener {
    @Resource
    private WfEmailLogService wfEmailLogService;
    @Resource
    private UserService userService;

    @RabbitListener(queues = ExchangeQueueConfig.WORK_ORDER_EMAIL_QUENE)
    public void message(String message) {
        log.info("MQ邮件内容{}", message);
        try {
            EmailDTO emailDTO = JSONObject.parseObject(message, EmailDTO.class);
            if (emailDTO != null) {
                SendEmail sendEmail = emailDTO.getSendEmail();
                List<String> userNoList = emailDTO.getUserNoList();
                PersonInfoWithAppReq req = new PersonInfoWithAppReq();
                req.setUserNoList(userNoList);
                List<AppUserResp> personInfos = userService.getPersonWithBusinessList(req);
                List<String> collect = personInfos.stream().map(AppUserResp::getEmail).collect(Collectors.toList());
                log.info("收件人：{}", collect);
                sendEmail.setTo(Joiner.on(",").join(collect));
                wfEmailLogService.sendEmail(sendEmail, emailDTO);
            }
        } catch (Exception e) {
            log.error("消费MQ信息失败（Email消息队列）！", e);
        }
    }


}
